<?php
class LoanNotification extends CWidget {

	public $dayOffset=25;
	public $column;
	public $title;

	public function run()
	{
		$column=$this->column;
		$dateFormat='Y-m-d';
		$criteria=new CDbCriteria();
		$currentTime=mktime (0, 0, 0 , date("n") ,date("j"),  date("Y") );

		$criteria->addBetweenCondition($column, date($dateFormat, $currentTime),
		date($dateFormat, $currentTime+86400*$this->dayOffset)
		);
		$criteria->order=$this->column.' asc';

		if(!Yii::app()->user->checkAccess('customerInfoView')){
			if(User::getCurrentUser()->teamLeader){
				$criteria->compare('teamId', User::getCurrentUser()->teamId);
			}else{
				$criteria->compare('createUserId', User::getCurrentUser()->id);				
			}
		}
		$criteria->with=array('customer');
		$loansDb=Loan::model()->findAll($criteria);
		$loans=array();
		foreach ($loansDb as $loanDb){
			$date=strtotime($loanDb->$column);
			$loans[]=array(
				'id'=>$loanDb->id,
				'customer'=>$loanDb->customer,
				'bank'=>$loanDb->bank,
				'date'=>$loanDb->$column,
				'daysLeft'=>intval(($date-$currentTime)/86400),
			);
		}
		$this->render('loanNotification',array('loans'=>$loans,'dayOffset'=>$this->dayOffset));
	}
}